﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace A2DFramework.SQLDispatcherService.Configuration
{
    public static class SQLDispatcherConfiguration
    {
        public static Dictionary<string, Connection> Connections = new Dictionary<string, Connection>();

        static SQLDispatcherConfiguration()
        {
            Connections = new Dictionary<string, Connection>();

            XElement root = XElement.Load(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "A2D.config"));

            root = root.Element("SQLDispatcher");
            if (root == null)
                throw new Exception("SQLDispatcher node not exists");

            foreach (var elmConn in root.Elements("Connection"))
            {
                Connection conn = new Connection();

                conn.WritableDB = null;
                conn.ReadDBs = new List<DB>();
                conn.DedicatedRegions = new List<DedicatedRegion>();

                conn.Name = elmConn.Attribute("Name").Value;

                conn.WritableDB = new DB() { ConnectionString = elmConn.Element("WritableDB").Value };

                foreach (var elm in elmConn.Element("ReadDBs").Elements("DB"))
                {
                    DB db = new DB();
                    db.ConnectionString = elm.Value;
                    conn.ReadDBs.Add(db);
                }
                if (elmConn.Element("DedicatedReadDBs") != null)
                {
                    foreach (var elm in elmConn.Element("DedicatedReadDBs").Elements("DedicatedRegion"))
                    {
                        DedicatedRegion region = new DedicatedRegion();

                        region.Region = elm.Element("Region").Value;
                        foreach (var elmInner in elm.Elements("DB"))
                        {
                            DB db = new DB();
                            db.ConnectionString = elmInner.Value;
                            region.DBs.Add(db);
                        }

                        conn.DedicatedRegions.Add(region);
                    }
                }

                if (conn.WritableDB == null || conn.WritableDB.ConnectionString.Length == 0)
                    throw new Exception("Please specify writable db info");
                if (conn.ReadDBs.Count == 0)
                    throw new Exception("Please specify readable db info");

                Connections.Add(conn.Name, conn);
            }
        }
    }

    public class Connection
    {
        public string Name { get; set; }
        public DB WritableDB { get; set; }
        public List<DB> ReadDBs { get; set; }
        public List<DedicatedRegion> DedicatedRegions { get; set; }
    }

    public class DB
    {
        //public int Weight { get; set; }
        public string ConnectionString { get; set; }
    }

    public class DedicatedRegion
    {
        public DedicatedRegion()
        {
            DBs = new List<DB>();
        }
        public string Region { get; set; }
        public List<DB> DBs { get; set; }
    }
}
